package com.xilihui.xlh.business.activitys;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.AppointmentEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.business.widget.CircleImageView;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseCompatFragment;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.glide.ImageHelper;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.TextStringUtils;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/25.
 */

public class StartAppointmentActivity extends BaseCompatActivity{
    
    @BindView(R.id.civ_userHead)
    SimpleDraweeView civ_userHead;
    @BindView(R.id.tv_xianmu)
    TextView tv_xianmu;
    @BindView(R.id.tv_zuoweiType)
    TextView tv_zuoweiType;
    @BindView(R.id.iv_qrcode)
    ImageView iv_qrcode;
    @BindView(R.id.tv_beizhu)
    TextView tv_beizhu;
    @BindView(R.id.tv_startTime)
    TextView tv_startTime;
    @BindView(R.id.tv_endTime)
    TextView tv_endTime;
    @BindView(R.id.tv_hint)
    TextView tv_hint;
    
    int status = 0;//0(开始工作) 1(结束工作) 2(取消预约)
    AppointmentEntity.DataBean.AppointBean data;
    String qrcode_url = "";
    
    int push_id = 0;
    int push_status = 0;
    
    @Override
    public int getLayout() {
        return R.layout.activity_start_appointment;
    }

    @Override
    public String initTitle() {
        return "扫描开始工作";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
       
    }

    @Override
    public void initData() {
        status = getIntent().getIntExtra("status",0);
        data = (AppointmentEntity.DataBean.AppointBean) getIntent().getSerializableExtra("data");
        if(status==0){
            setTitle("扫码开始工作");
            tv_startTime.setText("开始时间:等待确认开始");
            tv_endTime.setText("结束时间:等待确认开始");
            qrcode_url = UrlConst.baseUrl()+"api/appoint/begin_appoint?wid="+data.getId();
            tv_hint.setText("扫一扫上面的二维码，确认开始工作");
        }else if(status ==1){
            setTitle("扫码结束工作");
            tv_startTime.setText("开始时间:"+data.getCheck_start_time());
            tv_endTime.setText("结束时间:等待确认开始");
            qrcode_url = UrlConst.baseUrl()+"api/appoint/end_appoint?wid="+data.getId();
            tv_hint.setText("扫一扫上面的二维码，确认结束工作");
        }else if(status ==2){
            setTitle("扫码取消预约");
            qrcode_url = UrlConst.baseUrl()+"api/appoint/delete_appoint_store_code?id="+data.getId();
            tv_startTime.setVisibility(View.GONE);
            tv_endTime.setVisibility(View.GONE);
            tv_hint.setText("扫一扫上面的二维码，取消预约");
        }
       
        LogUtil.i("mylog","data:"+data.getId());
        LogUtil.i("mylog","head:"+UrlConst.baseUrl()+data.getAgent().getHeadimg());
        civ_userHead.setImageURI(UrlConst.baseUrl()+data.getAgent().getHeadimg());
        String xianmu = data.getHair_project().getName();
        String str = "预约项目："+xianmu;
        TextStringUtils.setSpanTextView(tv_xianmu,str,new TextStringUtils.SpanColorHolder(
                str.length()-xianmu.length(),str.length(),getResources().getColor(R.color.tv_333333)
        ));
        
        String seat = data.getSeat().getName();
        String str2 = "座位类型："+seat;
        TextStringUtils.setSpanTextView(tv_zuoweiType,str2,new TextStringUtils.SpanColorHolder(
                str2.length()-seat.length(),str2.length(),getResources().getColor(R.color.tv_333333)
        ));

        ImageHelper.display(this,iv_qrcode, qrcode_url);
        
        if(TextUtil.isEmpty(data.getRemark())){
            tv_beizhu.setVisibility(View.GONE);
        }else {
            tv_beizhu.setText(data.getRemark());
        }
        
        
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event){
        super.onEventMainThread(event);
        if(event.is(StrConst.UMENG_PUSH)){
            push_status = Integer.valueOf((String) event.getParams(0));
            switch (push_status){
                case 3:  //开始
                case 4:  //结束
                    int id = Integer.parseInt(data.getId());                    
                    tv_endTime.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    },1000);
                    
                    break;
                case 5:  //取消
                    
                    break;
            }
            
        }
        LogUtil.i("mylog","sdfsdfsdf");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
